package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UploadCategoryPeer extends BasePeer{
	public static final String tableName = "_Upload_Category";
	public static final String UPLOADID = "uploadId";
	public static final String CATEGORYID = "categoryId";
	public static final String[] columnNames=new String[]{"uploadId","categoryId"};
	public static UploadCategory getUploadCategoryByPKs(Object categoryId,Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadCategoryPeer.tableName,UploadCategoryPeer.columnNames," TRUE  AND `categoryId`=?  AND `uploadId`=? ",new Object[]{categoryId,uploadId,},0, 1,conn);
			UploadCategory uploadCategory = new UploadCategory();
		fill(rs,uploadCategory);
		return uploadCategory;
	}
	public static UploadCategory getUploadCategoryReferenceColumnsOnlyByPKs(Object categoryId,Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadCategoryPeer.tableName,new String[]{"categoryId","uploadId",}," TRUE AND `categoryId`=?AND `uploadId`=?",new Object[]{categoryId,uploadId,}, 0,  1,conn);
			UploadCategory uploadCategory = new UploadCategory();
		fill(rs,uploadCategory);
		return uploadCategory;
	}
	public static UploadCategory getUploadCategoryByUploadId(long uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadCategoryPeer.tableName,UploadCategoryPeer.columnNames,"`uploadId`=?",new Object[]{uploadId},0, 1,conn);
			UploadCategory uploadCategory = new UploadCategory();
		fill(rs,uploadCategory);
		return uploadCategory;
	}
	public static UploadCategory getUploadCategoryByCategoryId(long categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadCategoryPeer.tableName,UploadCategoryPeer.columnNames,"`categoryId`=?",new Object[]{categoryId},0, 1,conn);
			UploadCategory uploadCategory = new UploadCategory();
		fill(rs,uploadCategory);
		return uploadCategory;
	}
	public static void fill(ResultSet rs, UploadCategory uploadCategory) throws SQLException {
		if(rs.next()){
			try{uploadCategory.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{uploadCategory.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			uploadCategory.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UploadCategory> uploadCategoryCollection) throws SQLException {
		while(rs.next()){
			UploadCategory uploadCategory=new UploadCategory();
			try{uploadCategory.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{uploadCategory.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			uploadCategory.resetModified();
			uploadCategoryCollection.add(uploadCategory);
		}
	}
	public static boolean update(UploadCategory uploadCategory, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UploadCategoryPeer.columnNames.length; i++) {
			if (((UploadCategoryBase)uploadCategory).modified[i]) {
				columns.add(UploadCategoryPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_Upload_Category` SET ");
		sql.append("WHERE TRUE AND `categoryId` = ? AND `uploadId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,uploadCategory.get(column));
		}
		pstmt.setObject(columns.size()+1+0,uploadCategory.get("categoryId"));
		pstmt.setObject(columns.size()+1+1,uploadCategory.get("uploadId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UploadCategory uploadCategory, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (uploadCategory.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_Upload_Category` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (uploadCategory.get(column) != null) {
				pstmt.setObject(i + 1 + j, uploadCategory.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UploadCategory> getUploadCategoryCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UploadCategory> collection=new Vector<UploadCategory>();
		fill(rs,collection);
		return collection;
	}
}
